package com.tongzhan.controller;

import com.tongzhan.base.BaseController;
import com.tongzhan.base.Return;
import com.tongzhan.entity.party.NewPerson;
import com.tongzhan.entity.party.Notice;
import com.tongzhan.param.NewPersonParam;
import com.tongzhan.param.NoticeParam;
import com.tongzhan.utils.SnowFlakeUtils;
import com.tongzhan.vo.WorkInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 通知中心
 * @author htmic
 * @date 2020/7/30
 */
@RequestMapping("/notice")
@RestController
public class NoticeController extends BaseController {

    /**
     * 列表
     * @param page 页码
     * @param size 每页个数
     * @return
     */
    @PostMapping("/noticeList")
    public Return list(Integer page, Integer size) {
        page = page == null ? 0 : page - 1;
        Page<Notice> list = noticeService.getList(page, size);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list.getContent());
        map.put("total", list.getTotalElements());
        map.put("totalpage", list.getTotalPages());
        return Return.getSuccessResult(map);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @PostMapping("/noticetDetail")
    public Return getDetail(String id,String userId) {
        if (id == null) {
            return Return.getErrorResult("缺少id参数!");
        }
        Notice vo = noticeService.getDetail(id,userId);
        return Return.getSuccessResult(vo);
    }

    /**
     * 添加
     * @param param
     * @return
     */
    @PostMapping("/addNotice")
    public Return add(NoticeParam param) {
        if (param == null || param.getNotice_content() == null || param.getTitle() == null) {
            return Return.getErrorResult("参数为空");
        }
        if (param.getTitle().length() > 50) {
            return Return.getErrorResult("标题过长");
        }
        if (param.getNotice_content().length() > 500) {
            return Return.getErrorResult("内容不能为空");
        }
        if (StringUtils.isEmpty(param.getNotice_picture())) {
            return Return.getErrorResult("请上传图片");
        }

        Notice entity = new Notice();
        BeanUtils.copyProperties(param, entity);
        entity.setId(new SnowFlakeUtils(3, 3).nextId()+"");
        entity.setAddUserId(param.getUserId());
        long l = noticeService.add(entity);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }

    /**
     * 删除通知
     * @param id
     * @return
     */
    @PostMapping("/delNotice")
    public Return delNotice(String id) {
        if (id == null) {
            return Return.getMissParamResult();
        }
        //调用service方法
        long l = noticeService.delNotice(id);
        if (l > 0) {
            return Return.getSuccessResult("删除成功");
        }
        return Return.getErrorResult("删除失败");
    }
}
